home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / PERM_IDX.C < prev    next >
Text File  |  1991-09-17  |  1KB  |  38 lines

  1. /*
  2. ** Determine the permutation index for a given permutation list.
  3. ** Written by Thad Smith III, Boulder, CO  8/31/91
  4. ** Hereby contributed to the Public Domain.
  5. **
  6. ** The following function computes the ordinal of the given permutation,
  7. ** which is index of the permutation in sorting order:
  8. **  1, 2, ..., n-1, n   is index 0
  9. **  1, 2, ..., n, n-1   is index 1
  10. **  ...
  11. **  n, n-1, ..., 2, 1   is index n! -1
  12. **
  13. ** The actual values of the elements are immaterial, only the relative
  14. ** ordering of the values is used.
  15. **
  16. ** pit[] is the array of elements of length size.
  17. ** The return value is the permutation index.
  18. */
  19.  
  20. int perm_index (char pit[], int size)
  21. {
  22.       int i;
  23.       register int j, ball;
  24.       int index = 0;
  25.  
  26.       for (i = 1; i < size; i++)
  27.       {
  28.             ball = pit[i-1];
  29.             for (j = i; j < size; j++)
  30.             {
  31.                   if (ball > pit[j])
  32.                         index ++;
  33.             }
  34.             index *= size - i;
  35.       }
  36.       return index;
  37. }
  38.